package com.example.dataservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dataservice.dto.ExpenseListDto;
import com.example.dataservice.entity.Expense;
import com.example.dataservice.service.IExpenseService;
import com.example.dataservice.utils.Result;
import com.example.dataservice.vo.DestinationVo;
import com.example.dataservice.vo.ExpenseListVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-01-12
 */
@RestController
@RequestMapping("/api/expense")
public class ExpenseController {
	@Resource
	IExpenseService expenseService;
	
	private static final String[] DESTINATIONS = {"工资结款","订单支付","比赛支出","其他支出"};
	@PostMapping("/list")
	public Result<ExpenseListVo> list(@RequestBody ExpenseListDto expenseListDto) {
		Page<Expense> page = new Page<>(expenseListDto.getPage(), expenseListDto.getLimit());
		LambdaQueryWrapper<Expense> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(expenseListDto.getExpenseDate()!=null,
					Expense::getExpenseDate, expenseListDto.getExpenseDate())
					.eq(expenseListDto.getDestination()!=null&&!expenseListDto.getDestination().equals("")
						&&!expenseListDto.getDestination().equals("请选择"),
					Expense::getDestination, expenseListDto.getDestination())
					.eq(expenseListDto.getCategory()!=null&&!expenseListDto.getCategory().equals("")
						&&!expenseListDto.getCategory().equals("请选择"),
					Expense::getCategory, expenseListDto.getCategory());
		expenseService.page(page,queryWrapper);
		ExpenseListVo expenseListVo = new ExpenseListVo();
		expenseListVo.setExpenses(page.getRecords());
		expenseListVo.setCount(page.getTotal());
		return Result.success(expenseListVo);
	}
	@PostMapping("/save")
	public Result<String> save(@RequestBody Expense expense) {
		if(expense.getId()!=null){
			expenseService.updateById(expense);
			return Result.success("修改成功");
		}
		expenseService.save(expense);
		return Result.success("保存成功");
	}
	@DeleteMapping("/delete/{id}")
	public Result<String> delete(@PathVariable Long id) {
		if(id!=null&&expenseService.getById(id)!=null){
			expenseService.removeById(id);
			return Result.success("删除成功");
		}
		return Result.fail("删除失败");
	}
	
	@GetMapping("/getExpenses")
	public Result<List<Long>> getExpenses(String year) {
		String minDate = year+"-01-01";
		String maxDate = year+"-12-31";
		List<Expense> list = expenseService.list(new LambdaQueryWrapper<Expense>()
				.ge(Expense::getExpenseDate, minDate)
				.le(Expense::getExpenseDate, maxDate));
		List<Long> countList = new ArrayList<>();
		for (int i = 1;i<=12;i++){
			long res = 0L;
			for (Expense expense : list){
				if(expense.getExpenseDate().getMonthValue()==i) res+=expense.getAmount().intValue();
			}
			countList.add(res);
		}
		return Result.success(countList);
	}
	@GetMapping("/getDestination")
	public Result<List<DestinationVo>> getExpensesByDate(String year) {
		String minDate = year+"-01-01";
		String maxDate = year+"-12-31";
		List<Expense> list = expenseService.list(new LambdaQueryWrapper<Expense>()
			.ge(Expense::getExpenseDate, minDate)
			.le(Expense::getExpenseDate, maxDate));
		List<DestinationVo> destinationVoList = new ArrayList<>();
		for (String distination : DESTINATIONS){
			long res = 0L;
			 for(Expense expense : list){
				 if(expense.getDestination().equals(distination)) res++;
			 }
			 destinationVoList.add(new DestinationVo(distination,res));
		}
		return Result.success(destinationVoList);
	}
}
